package com.example.administrator.basedemos.activity;

import android.app.Dialog;
import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.adapter.OptinalEdtAdapter;
import com.example.administrator.basedemos.bean.OptinalBean;
import com.example.administrator.basedemos.utils.LogUtil;
import com.example.administrator.basedemos.utils.RecycleUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者     ZCL
 * 创建时间   2018/7/20 15:12
 * 描述	      ${编辑自选}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class OptinalEdtActivity extends BaseZcActivity implements BaseQuickAdapter.OnItemChildClickListener, View.OnClickListener {
    private static final String TAG = "OptinalEdtActivity";
    @BindView(R.id.optional_edt_recyclerview)
    RecyclerView mOptionalEdtRecyclerview;
    @BindView(R.id.optional_edt_top)
    TextView     mOptionalEdtTop;
    @BindView(R.id.optional_edt_bottom)
    LinearLayout mOptionalEdtBottom;

    private ArrayList<OptinalBean> mList;
    private OptinalEdtAdapter      mAdapter;
    private ArrayList<OptinalBean> mListDel;
    private Dialog                 mDialog;
    private TextView               submitTxt;
    private TextView               cancelTxt;
    private TextView               contentText;
    private int dialogType = 0;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_optional_edt;
    }

    @Override
    protected void initData() {
        mDialog = new Dialog(this);
        View view = View.inflate(this, R.layout.dialog_get_at_once, null);
        contentText = (TextView) view.findViewById(R.id.content_once);
        submitTxt = (TextView) view.findViewById(R.id.submit_once);
        cancelTxt = (TextView) view.findViewById(R.id.cancel_once);
        submitTxt.setText(R.string.确认);
        cancelTxt.setText(R.string.取消);


        mDialog.setContentView(view);


        mListDel = new ArrayList<OptinalBean>();
        setToolbarBackGroundColor(R.color.yellow);
        setToolBarTitle(getString(R.string.编辑自选));
        mSetting.setVisibility(View.GONE);
        mConfirm.setVisibility(View.VISIBLE);
        RecycleUtil.setLayoutMangerVertical(mOptionalEdtRecyclerview, this);
        mList = new ArrayList<OptinalBean>();
        mList.add(new OptinalBean(false, "1"));
        mList.add(new OptinalBean(false, "2"));
        mList.add(new OptinalBean(false, "3"));
        mList.add(new OptinalBean(false, "4"));
        mList.add(new OptinalBean(false, "5"));
        mList.add(new OptinalBean(false, "6"));

        mOptionalEdtTop.setVisibility(mList.size() > 0 ? View.VISIBLE : View.GONE);
        mOptionalEdtBottom.setVisibility(mList.size() > 0 ? View.VISIBLE : View.GONE);

        mAdapter = new OptinalEdtAdapter(R.layout.item_optinal_edt, mList);
        View emptyView = View.inflate(this, R.layout.empty_view, null);
        ImageView emptyIv = emptyView.findViewById(R.id.empty_iv);
        TextView emptyTv = emptyView.findViewById(R.id.empty_title);
        emptyIv.setImageResource(R.mipmap.empty_box);
        emptyTv.setText(R.string.当前无添加的自选);
        mAdapter.setEmptyView(emptyView);
        mOptionalEdtRecyclerview.setAdapter(mAdapter);
        //创建回调
        ItemDragAndSwipeCallback dragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
        //添加帮助
        ItemTouchHelper touchHelper = new ItemTouchHelper(dragAndSwipeCallback);
        //关联
        touchHelper.attachToRecyclerView(mOptionalEdtRecyclerview);
        //设置拖拽焦点是都指定某控件及长按
        mAdapter.enableDragItem(touchHelper, R.id.item_optional_edt_move, false);
        mAdapter.setOnItemDragListener(itemDragListener);
        mAdapter.setOnItemChildClickListener(this);

        //设置侧滑删除
        //mAdapter.enableSwipeItem();
        //mAdapter.setOnItemSwipeListener(itemSwipeListener);
        //mAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void initListener() {
        super.initListener();
        mConfirm.setOnClickListener(this);
        submitTxt.setOnClickListener(this);
        cancelTxt.setOnClickListener(this);
        mToolbar.setNavigationOnClickListener(this);
    }

    @OnClick({R.id.optional_edt_del, R.id.optional_edt_all})
    public void OnClik(View v) {
        mListDel.clear();
        switch (v.getId()) {
            case R.id.optional_edt_del:
                mListDel.clear();
                for (OptinalBean bean : mList) {
                    if (bean.isCheckEd())
                        mListDel.add(bean);
                }
                contentText.setText(getString(R.string.确认删除));
                dialogType = 2;
                if (mListDel.size() > 0)
                    mDialog.show();
                else
                    Toast.makeText(getApplicationContext(), "没有要删除的自选..", Toast.LENGTH_SHORT).show();
                break;
            case R.id.optional_edt_all:
                for (OptinalBean bean : mList) {
                    bean.setCheckEd(true);
                }
                if (mAdapter != null) {
                    mAdapter.notifyDataSetChanged();
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        OptinalBean s = mList.get(position);
        switch (view.getId()) {
            case R.id.item_optional_edt_top:
                if (mAdapter != null) {
                    mList.remove(position);
                    mList.add(0, s);
                    mAdapter.notifyDataSetChanged();
                }
                break;

            case R.id.item_optional_edt_star:
                if (mAdapter != null) {
                    s.setCheckEd(s.isCheckEd() ? false : true);
                    mAdapter.notifyDataSetChanged();
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.z_toolbar_confirm:
                //todo 保存当前界面返回上一页
                finish();
                break;
            case R.id.submit_once:
                if (dialogType == 1)
                    finish();
                else if (dialogType == 2) {
                    mList.removeAll(mListDel);
                    mAdapter.notifyDataSetChanged();
                }
                mDialog.dismiss();
                break;
            case R.id.cancel_once:
                mDialog.dismiss();
                break;
            case R.id.z_toolbar:
                contentText.setText(getString(R.string.确认舍弃此次编辑));
                dialogType = 1;
                mDialog.show();
                break;

            default:
                break;
        }
    }

    OnItemDragListener itemDragListener = new OnItemDragListener() {
        @Override
        public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
        }

        @Override
        public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
        }

        @Override
        public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
            LogUtil.util(TAG, "返回数据是:" + mList.toString());
        }
    };

    OnItemSwipeListener itemSwipeListener = new OnItemSwipeListener() {
        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
        }

        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
        }

        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
        }

        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
        }
    };
}
